<?php

class ecTemplateTagHelper{
  /**
   * Constructs an html tag.
   *
   * @param  string $name     tag name
   * @param  array  $options  tag options
   * @param  bool   $open     true to leave tag open
   * @return string
   */
  public static function tag($name, $options = array(), $open = false)
  {
    if (!$name)
    {
      return '';
    }

    return '<'.$name.self::_tag_options($options).(($open) ? '>' : ' />');
  }

  public static function content_tag($name, $content = '', $options = array())
  {
    if (!$name)
    {
      return '';
    }

    return '<'.$name.self::_tag_options($options).'>'.$content.'</'.$name.'>';
  }

  public static function cdata_section($content)
  {
    return "<![CDATA[$content]]>";
  }

  /**
   * Wraps the content in conditional comments.
   *
   * @param  string $condition
   * @param  string $content
   *
   * @return string
   *
   * @see http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx
   */
  public static function comment_as_conditional($condition, $content)
  {
    return "<!--[if $condition]>$content<![endif]-->";
  }

  /**
   * Escape carrier returns and single and double quotes for Javascript segments.
   */
  public static function escape_javascript($javascript = '')
  {
    $javascript = preg_replace('/\r\n|\n|\r/', "\\n", $javascript);
    $javascript = preg_replace('/(["\'])/', '\\\\\1', $javascript);

    return $javascript;
  }

  /**
   * Escapes an HTML string.
   *
   * @param  string $html HTML string to escape
   * @return string escaped string
   */
  public static function escape_once($html)
  {
    return self::fix_double_escape(htmlspecialchars($html, ENT_COMPAT, ecConfiguration::getCharset()));
  }

  /**
   * Fixes double escaped strings.
   *
   * @param  string $escaped HTML string to fix
   * @return string fixed escaped string
   */
  public static function fix_double_escape($escaped)
  {
    return preg_replace('/&amp;([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', $escaped);
  }

  public static function _tag_options($options = array())
  {
    $options = self::_parse_attributes($options);

    $html = '';
    foreach ($options as $key => $value)
    {
      $html .= ' '.$key.'="'.self::escape_once($value).'"';
    }

    return $html;
  }

  public static function _parse_attributes($string)
  {
    return is_array($string) ? $string : sfToolkit::stringToArray($string);
  }

  public static function _get_option(&$options, $name, $default = null)
  {
    if (array_key_exists($name, $options))
    {
      $value = $options[$name];
      unset($options[$name]);
    }
    else
    {
      $value = $default;
    }

    return $value;
  }

  /**
   * Returns a formatted ID based on the <i>$name</i> parameter and optionally the <i>$value</i> parameter.
   *
   * This function determines the proper form field ID name based on the parameters. If a form field has an
   * array value as a name we need to convert them to proper and unique IDs like so:
   * <samp>
   *  name[] => name (if value == null)
   *  name[] => name_value (if value != null)
   *  name[bob] => name_bob
   *  name[item][total] => name_item_total
   * </samp>
   *
   * <b>Examples:</b>
   * <code>
   *  echo get_id_from_name('status[]', '1');
   * </code>
   *
   * @param  string $name   field name
   * @param  string $value  field value
   *
   * @return string <select> tag populated with all the languages in the world.
   */
  public static function get_id_from_name($name, $value = null)
  {
    // check to see if we have an array variable for a field name
    if (strstr($name, '['))
    {
      $name = str_replace(array('[]', '][', '[', ']'), array((($value != null) ? '_'.$value : ''), '_', '_', ''), $name);
    }

    return $name;
  }

  /**
   * Converts specific <i>$options</i> to their correct HTML format
   *
   * @param  array $options
   * @return array returns properly formatted options
   */
  public static function _convert_options($options)
  {
    $options = self::_parse_attributes($options);

    foreach (array('disabled', 'readonly', 'multiple') as $attribute)
    {
      if (array_key_exists($attribute, $options))
      {
        if ($options[$attribute])
        {
          $options[$attribute] = $attribute;
        }
        else
        {
          unset($options[$attribute]);
        }
      }
    }

    return $options;
  }
  
}

?>
